package com.my.blogapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.my.blogapi.dao.mapper.CommentMapper;
import com.my.blogapi.dao.pojo.Comment;
import com.my.blogapi.dao.pojo.SysUser;
import com.my.blogapi.service.CommentService;
import com.my.blogapi.service.SysUserService;
import com.my.blogapi.utils.UserThreadLoacl;
import com.my.blogapi.vo.CommentVo;
import com.my.blogapi.vo.UserVo;
import com.my.blogapi.vo.param.CommentParam;
import com.my.blogapi.vo.param.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private SysUserService sysUserService;


    /**
     * 根据文章ID查询对应的评论信息
     *
     * @param id
     * @return
     */
    @Override
    public Result findCommentByArticle(Long id) {
        /**
         * 1.根据文章id查询评论列表，从comment表中查询
         * 2.根据作者id查询作者信息
         * 3.判断如果level = 1 要去查询他有没有子评论
         * 4.如果有 根据评论id进行查询（parent_id）
         */
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId, id);
        queryWrapper.eq(Comment::getLevel, 1);
        List<Comment> commentList = commentMapper.selectList(queryWrapper);
        List<CommentVo> commentVoList = copyList(commentList);
        return Result.success(commentVoList);
    }

    @Override
    public Result comment(CommentParam commentParam) {
        SysUser sysUser = UserThreadLoacl.get();
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParam.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        this.commentMapper.insert(comment);
        return Result.success(null);
    }

    private List<CommentVo> copyList(List<Comment> commentList) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment, commentVo);
        //2.根据作者id查询作者信息
        Long authorId = comment.getAuthorId();
        UserVo userVo = sysUserService.findSysUserVoByAuthorId(authorId);
        commentVo.setAuthor(userVo);
        //子评论
        Integer level = comment.getLevel();
        if (1 == level) {
            //获取评论ID
            Long id = comment.getId();
            //把该ID下的所有子评论查询出来
            List<CommentVo> commentVoList = findCommentByParentId(id);
            commentVo.setChildrens(commentVoList);
        }
        //给谁评论
        Long toUid = comment.getToUid();
        if (level > 1) {
            UserVo toUserVo = sysUserService.findSysUserVoByAuthorId(toUid);
            commentVo.setToUser(toUserVo);
        }
        return commentVo;
    }

    private List<CommentVo> findCommentByParentId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId, id);
        queryWrapper.eq(Comment::getLevel, 2);
        List<Comment> commentList = commentMapper.selectList(queryWrapper);
        return copyList(commentList);
    }

}
